#include "../../util/macros.h"

#include "../image.h"

void image_histogram(Image * Gray, Matrix ** Hist, int normalize) {

	unsigned char * gray;
	double * hist;

	long int imsize;

	_verify(Gray);
	_verify(Hist);

	gray = Gray->data;
	imsize = Gray->size;

	if (*Hist == NULL) (*Hist) = matrix_new(1, 256);
	hist = (*Hist)->data;

	{ /* Histogram calculation ============================================== */
		long int i;
		int px;
		for (i = 0; i < imsize; i++) {
			px = gray[i];
			hist[px]++;
		}
	}
	{ /* Normalization ====================================================== */
		int i;
		if (normalize) {
			for (i = 0; i < 256; i++) {
				hist[i] = hist[i] / (double) imsize;
			}
		}
	}

}
